package com.inspur.edp.common.type.deserializer;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.inspur.edp.common.type.exception.TypeException;

import java.util.ArrayList;

/**
 * @author lizhaorui
 * @date 2025/9/28
 * @description
 */
public class DefaultListDeserializer implements TypeDeserializer {

    private TypeDeserializer itemDeserializer;

    public DefaultListDeserializer(TypeDeserializer itemDeserializer) {
        this.itemDeserializer = itemDeserializer;
    }

    @Override
    public Object deserialize(JsonNode jsonNode) {
        if (jsonNode == null) {
            return null;
        }
        if (!(jsonNode instanceof ArrayNode)) {
            throw new TypeException("parameter is not ArrayNode type.");
        }
        ArrayList arrayList = new ArrayList();
        for (JsonNode itemNode : jsonNode) {
            Object itemObject = itemDeserializer.deserialize(itemNode);
            arrayList.add(itemObject);
        }
        return arrayList;
    }

}